#include "qtoolgraphicsscene.h"
#include "qcurvetool.h"
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include "..\SceneData\ShapeNode.h"
QToolGraphicsScene::QToolGraphicsScene(QObject *parent)
    :QGraphicsScene(parent),currentTool(0),
	ToolDict(),itemsMap(),currentTime(0),ContainerEnter(0),controller(0)
	,CurrentBrush(QColor(0,0,0,0)),CurrentPen(Qt::SolidLine)
{
	CurrentPen.setWidthF(1);
}

QToolGraphicsScene::QToolGraphicsScene(const QRectF & sceneRect, QObject * parent )
    : QGraphicsScene(sceneRect,parent),currentTool(0),
	ToolDict(),itemsMap(),currentTime(0),ContainerEnter(0),controller(0)
	,CurrentBrush(QColor(0,0,0,0)),CurrentPen(Qt::SolidLine)
{
	CurrentPen.setWidthF(1);
}
QToolGraphicsScene::QToolGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject * parent )
    : QGraphicsScene(x,y,width,height,parent),currentTool(0),
	ToolDict(),itemsMap(),currentTime(0),ContainerEnter(0),controller(0)
	,CurrentBrush(QColor(0,0,0,0)),CurrentPen(Qt::SolidLine)
{
	CurrentPen.setWidthF(1);
}



QToolGraphicsScene::~QToolGraphicsScene()
{
    for(QMap<QString,QMyTool*>::iterator iter=ToolDict.begin();iter!=ToolDict.end();++iter)
        delete iter.value();
}




void QToolGraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
    if(currentTool)
        currentTool->mouseDoubleClickEvent(mouseEvent);

}
void QToolGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mousePressEvent(mouseEvent);
    if(currentTool)
        currentTool->mousePressEvent(mouseEvent);

}
void QToolGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mouseMoveEvent(mouseEvent);
    if(currentTool)
        currentTool->mouseMoveEvent(mouseEvent);
}
void QToolGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
    if(currentTool)
        currentTool->mouseReleaseEvent(mouseEvent);
}
void QToolGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
{
    QGraphicsScene::contextMenuEvent(contextMenuEvent);
    if(currentTool)
        currentTool->contextMenuEvent(contextMenuEvent);
}

bool QToolGraphicsScene::AddTool(QString name,QMyTool * tool_pt)
{
    if(tool_pt)
    {
		tool_pt->SetController(this->controller);
		tool_pt->SetContainer(this);
        ToolDict.insert(name,tool_pt);
        return true;
    }
    return false;
}

bool QToolGraphicsScene::SetCurrentTool(QString name)
{
    if(ToolDict.contains(name))
    {
		if(currentTool)
			currentTool->Finalize();
        currentTool=ToolDict[name];
		currentTool->Initialize();
        return true;
    }
    return false;
}

void QToolGraphicsScene::SetTime(int time)
{
	currentTime=time;
	for(QMap<QGraphicsItem*,int>::iterator iter=itemsMap.begin();
		iter!=itemsMap.end();++iter)
	{
		int i=iter.value();
		double Posx=controller->GetValueByIndex(i,posx,currentTime);
		double Posy=controller->GetValueByIndex(i,posy,currentTime);
		double Sx=controller->GetValueByIndex(i,scalex,currentTime);
		double Sy=controller->GetValueByIndex(i,scaley,currentTime);
		double Ax=controller->GetValueByIndex(i,axisx,currentTime);
		double Ay=controller->GetValueByIndex(i,axisy,currentTime);
		double Sh=controller->GetValueByIndex(i,skewx,currentTime)/180*3.14159;
		double Sv=controller->GetValueByIndex(i,skewy,currentTime)/180*3.14159;
		double A=controller->GetValueByIndex(i,angle,currentTime)/180*3.14159;
		double AL=controller->GetValueByIndex(i,alpha,currentTime);
		switch(controller->GetNodeType(i))
		{
		case MovieClip:
		case Symbol:
		case Picture:
		case WordShape:
			{
				QImage temp(controller->ImageOffsetX(i)+controller->GetObjectImageByIndex(i).width()
					,controller->ImageOffsetY(i)+controller->GetObjectImageByIndex(i).height()
					,QImage::Format_ARGB32);
				temp.fill(0);
				QPainter painter(&temp);
				painter.drawImage(controller->ImageOffsetX(i)
					,controller->ImageOffsetY(i)
					,controller->GetObjectImageByIndex(i));
				painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
				painter.fillRect(0,0
					,controller->ImageOffsetX(i)+controller->GetObjectImageByIndex(i).width()
					,controller->ImageOffsetY(i)+controller->GetObjectImageByIndex(i).height(),
					QColor(0, 0, 0, AL*255));
				QPixmap pmp;
				pmp.convertFromImage(temp);
				((QGraphicsPixmapItem*)(iter.key()))->setPixmap(pmp);
			}
			break;
		default:
			{
				QPen pen=((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef();
				QColor color=pen.color();
				color.setAlphaF(color.alphaF()*AL);
				pen.setColor(color);
				((QAbstractGraphicsShapeItem*)(iter.key()))->setPen(pen);
				QBrush brush=((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef();
				color=brush.color();
				color.setAlphaF(color.alphaF()*AL);
				brush.setColor(color);
				((QAbstractGraphicsShapeItem*)(iter.key()))->setBrush(brush);
			}
			break;
		}
		iter.key()->setPos(0,0);
		QTransform trans;
		trans=QTransform(1,0,0,1,Posx,Posy)*trans;
		trans=QTransform(cos(Sv), sin(Sv), -sin(Sh), cos(Sh),0,0)*trans;
		trans=QTransform(cos(A)*Sx, sin(A)*Sx, -sin(A)*Sy, cos(A)*Sy,0,0)*trans;
		trans=QTransform(1,0, 0,1,-Ax,-Ay)*trans;
		iter.key()->setTransform(trans);
	}
}


void QToolGraphicsScene::AddPixmapItem(int node_index)
{
	double Posx=controller->GetValueByIndex(node_index,posx);
	double Posy=controller->GetValueByIndex(node_index,posy);
	double Sx=controller->GetValueByIndex(node_index,scalex);
	double Sy=controller->GetValueByIndex(node_index,scaley);
	double Ax=controller->GetValueByIndex(node_index,axisx);
	double Ay=controller->GetValueByIndex(node_index,axisy);
	double Sh=controller->GetValueByIndex(node_index,skewx)/180*3.14159;
	double Sv=controller->GetValueByIndex(node_index,skewy)/180*3.14159;
	double A=controller->GetValueByIndex(node_index,angle)/180*3.14159;
	double AL=controller->GetValueByIndex(node_index,alpha);
	QImage temp(controller->ImageOffsetX(node_index)+controller->GetObjectImageByIndex(node_index).width()
		,controller->ImageOffsetY(node_index)+controller->GetObjectImageByIndex(node_index).height()
		,QImage::Format_ARGB32);
	temp.fill(0);
	QPainter painter(&temp);
	painter.drawImage(controller->ImageOffsetX(node_index)
		,controller->ImageOffsetY(node_index)
		,controller->GetObjectImageByIndex(node_index));
	painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
	painter.fillRect(0,0
		,controller->ImageOffsetX(node_index)+controller->GetObjectImageByIndex(node_index).width()
		,controller->ImageOffsetY(node_index)+controller->GetObjectImageByIndex(node_index).height(),
		QColor(0, 0, 0, AL*255));
	QPixmap pmp;
	pmp.convertFromImage(temp);
	QGraphicsPixmapItem *item=new QGraphicsPixmapItem(pmp,0,this);
	AddItemLink(item,node_index);
	QTransform trans;
	trans=QTransform(cos(Sv), sin(Sv), -sin(Sh), cos(Sh),Posx,Posy)*trans;
	trans=QTransform(cos(A)*Sx, sin(A)*Sx, -sin(A)*Sy, cos(A)*Sy,0,0)*trans;
	trans=QTransform(1,0, 0,1,-Ax,-Ay)*trans;
	item->setTransform(trans);
	item->setFlag(QGraphicsItem::ItemIsSelectable, true);
	item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
	item->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
}

void QToolGraphicsScene::SetCurrentContainer(nodePath path)
{
	ContainerEnter=0;
	currentTime=0;
	this->clear();
	itemsMap.clear();
	if(!controller)
		throw "error";
	controller->receiveMessage(new SelectContainerMessage(path));
	for(int i=0;i!=controller->NumberOfObjects();++i)
	{
		double Posx=controller->GetValueByIndex(i,posx);
		double Posy=controller->GetValueByIndex(i,posy);
		double Sx=controller->GetValueByIndex(i,scalex);
		double Sy=controller->GetValueByIndex(i,scaley);
		double Ax=controller->GetValueByIndex(i,axisx);
		double Ay=controller->GetValueByIndex(i,axisy);
		double Sh=controller->GetValueByIndex(i,skewx)/180*3.14159;
		double Sv=controller->GetValueByIndex(i,skewy)/180*3.14159;
		double A=controller->GetValueByIndex(i,angle)/180*3.14159;
		double AL=controller->GetValueByIndex(i,alpha);
		QGraphicsItem *item=0;
		switch(controller->GetNodeType(i))
		{
		case MovieClip:
		case Symbol:
		case Picture:
		case WordShape:
			{
				QImage temp(controller->ImageOffsetX(i)+controller->GetObjectImageByIndex(i).width()
					,controller->ImageOffsetY(i)+controller->GetObjectImageByIndex(i).height()
					,QImage::Format_ARGB32);
				temp.fill(0);
				QPainter painter(&temp);
				painter.drawImage(controller->ImageOffsetX(i)
					,controller->ImageOffsetY(i)
					,controller->GetObjectImageByIndex(i));
				painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
				painter.fillRect(0,0
					,controller->ImageOffsetX(i)+controller->GetObjectImageByIndex(i).width()
					,controller->ImageOffsetY(i)+controller->GetObjectImageByIndex(i).height(),
					QColor(0, 0, 0, AL*255));
				QPixmap pmp;
				pmp.convertFromImage(temp);
				item=new QGraphicsPixmapItem(pmp,0,this);
				((QGraphicsPixmapItem*)item)->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
			}
			break;
		case Rect:
			{
				RectNode *node=(RectNode*)(controller->GetObjectByIndex(i));
				item =new QGraphicsRectItem(0,0,node->GetWidth(),node->GetHeight(),0,this);
				QPen pen=((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef();
				QColor color=pen.color();
				color.setAlphaF(color.alphaF()*AL);
				pen.setColor(color);
				((QGraphicsRectItem*)item)->setPen(pen);
				QBrush brush=((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef();
				color=brush.color();
				color.setAlphaF(color.alphaF()*AL);
				brush.setColor(color);
				((QGraphicsRectItem*)item)->setBrush(brush);
			}
			break;
		case Eclipse:
			{
				EclipseNode *node1=(EclipseNode*)(controller->GetObjectByIndex(i));
				item =new QGraphicsEllipseItem(0,0,node1->GetWidth(),node1->GetHeight(),0,this);
				QPen pen=((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef();
				QColor color=pen.color();
				color.setAlphaF(color.alphaF()*AL);
				pen.setColor(color);
				((QGraphicsEllipseItem*)item)->setPen(pen);
				QBrush brush=((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef();
				color=brush.color();
				color.setAlphaF(color.alphaF()*AL);
				brush.setColor(color);
				((QGraphicsEllipseItem*)item)->setBrush(brush);
			}
			break;
		case Path:
			{
				PathNode * node2=(PathNode*)(controller->GetObjectByIndex(i));
				item=new QGraphicsPathItem(node2->getPath(),0,this);
				QPen pen=((ShapeNode*)(controller->GetObjectByIndex(i)))->LinePenRef();
				QColor color=pen.color();
				color.setAlphaF(color.alphaF()*AL);
				pen.setColor(color);
				((QGraphicsPathItem*)item)->setPen(pen);
				QBrush brush=((ShapeNode*)(controller->GetObjectByIndex(i)))->FillBrushRef();
				color=brush.color();
				color.setAlphaF(color.alphaF()*AL);
				brush.setColor(color);
				((QGraphicsPathItem*)item)->setBrush(brush);
			}
			break;
		}
		itemsMap.insert(item,i);
		QTransform trans;
		item->setPos(0,0);
		trans=QTransform(cos(Sv), sin(Sv), -sin(Sh), cos(Sh),Posx,Posy)*trans;
		trans=QTransform(cos(A)*Sx, sin(A)*Sx, -sin(A)*Sy, cos(A)*Sy,0,0)*trans;
		trans=QTransform(1,0, 0,1,-Ax,-Ay)*trans;
		item->setTransform(trans);
		//item->setFlag(QGraphicsItem::ItemIsMovable, true);
		item->setFlag(QGraphicsItem::ItemIsSelectable, true);
		item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
	}
}